﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace UI.Grid
{
    public delegate void PageIndexChanged(object sender, int pageIndex);

    public partial class DataNavigator : UserControl
    {
        public event PageIndexChanged OnPageIndexChanged;

        public DataNavigator()
        {
            InitializeComponent();
        }

        #region props
        private EntityGridView _AttachedGrid;
        /// <summary></summary>
        public EntityGridView AttachedGrid
        {
            get
            {
                return this._AttachedGrid;
            }
            set
            {
                this._AttachedGrid = value;
            }
        }

        /// <summary></summary>
        public string AttachedGridName
        {
            get
            {
                if (this._AttachedGrid != null)
                    return this._AttachedGrid.Name;
                else
                    return null;
            }
            set
            {
                if(!string.IsNullOrEmpty(value))
                {
                    EntityGridView grid = this.FindName(value) as EntityGridView;
                    this._AttachedGrid = grid;
                }
            }
        }

        /// <summary></summary>
        public int Total
        {
            get
            {
                if (this._AttachedGrid != null)
                    return this._AttachedGrid.TotalRecords;
                else
                    return 0;
            }
        }

        public int PageSize
        {
            get
            {
                if (this._AttachedGrid != null)
                    return this._AttachedGrid.PageSize;
                else
                    return 20;
            }
        }

        /// <summary></summary>
        public int Current
        {
            get
            {
                if (this._AttachedGrid != null)
                    return this._AttachedGrid.CurrentPageIndex;
                else
                    return 0;
            }
            set
            {
                if (this.Current != value)
                {
                    if (this._AttachedGrid != null)
                    {
                        this._AttachedGrid.CurrentPageIndex = value;
                        if (this.OnPageIndexChanged != null)
                            this.OnPageIndexChanged(this, this.Current);
                    }
                }
                this.DataBind();
            }
        }
        #endregion

        #region data binding
        public void DataBind()
        {
            if(this.Total>0 && this.Current>=0 && this.Current<this.Total)
            {
                this.txtCurrent.Text = (this.Current + 1).ToString();
                if(this.Current>0)
                {
                    this.linkPrev.IsEnabled = true;
                    this.linkFirst.IsEnabled = true;
                    this.linkPrev.Foreground = new SolidColorBrush(Colors.Blue);
                    this.linkFirst.Foreground = new SolidColorBrush(Colors.Blue);
                }
                else
                {
                    this.linkPrev.IsEnabled = false;
                    this.linkFirst.IsEnabled = false;
                    this.linkPrev.Foreground = new SolidColorBrush(Colors.Gray);
                    this.linkFirst.Foreground = new SolidColorBrush(Colors.Gray);
                }
                if (this.Current < this.GetTotalPages(this.Total, this.PageSize) - 1)
                {
                    this.linkNext.IsEnabled = true;
                    this.linkLast.IsEnabled = true;
                    this.linkNext.Foreground = new SolidColorBrush(Colors.Blue);
                    this.linkLast.Foreground = new SolidColorBrush(Colors.Blue);
                }
                else
                {
                    this.linkNext.IsEnabled = false;
                    this.linkLast.IsEnabled = false;
                    this.linkNext.Foreground = new SolidColorBrush(Colors.Gray);
                    this.linkLast.Foreground = new SolidColorBrush(Colors.Gray);
                }
            }
        }
        #endregion

        private void linkFirst_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if(this.Total >0 && this.Current>0)
            {
                this.Current = 0;
            }
        }

        private void linkPrev_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.Total > 0 && this.Current > 0)
            {
                this.Current -= 1;
            }
        }

        private void txtCurrent_KeyDown(object sender, KeyEventArgs e)
        {
            this.Current = int.Parse(this.txtCurrent.Text);
        }

        private void linkNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.Total > 0 && this.PageSize>0 && 
                this.Current < this.GetTotalPages(this.Total, this.PageSize) -1)
            {
                this.Current += 1;
            }
        }

        private void linkLast_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.Total > 0 && this.PageSize>0 && 
                this.Current < this.GetTotalPages(this.Total, this.PageSize) - 1)
            {
                this.Current = this.GetTotalPages(this.Total, this.PageSize) - 1;
            }
        }

        private int GetTotalPages(int totalRecords, int pageSize)
        {
            return (int) Math.Ceiling((double) totalRecords/pageSize);
        }
    }
}
